package com.example.whatrubbish.service.impl;

import com.example.whatrubbish.mapper.CommodityMapper;
import com.example.whatrubbish.model.Commodity;
import com.example.whatrubbish.model.CommodityDetail;
import com.example.whatrubbish.service.CommodityDetailService;
import com.example.whatrubbish.service.CommodityService;
import com.example.whatrubbish.utils.DemoException;
import com.example.whatrubbish.vo.CommodityDetailVO;
import com.example.whatrubbish.vo.Criteria;
import com.example.whatrubbish.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    CommodityMapper commodityMapper;
    @Autowired
    CommodityDetailService commodityDetailService;

    @Override
    public PageBean<CommodityDetailVO> findCommodityWithPage(String title, Integer status, Integer pageIndex, Integer pageSize) {
        Criteria criteria = new Criteria();
        criteria.addExtField("title", title);
        criteria.addExtField("status", status);
        criteria.addExtField("pageIndex", pageIndex <= 1 ? 0 :(pageIndex - 1) * pageSize);
        criteria.addExtField("pageSize", pageSize);
        Long count = commodityMapper.selectWithPageCount(criteria);
        List<Commodity> list = commodityMapper.selectWithPage(criteria);
        List<CommodityDetailVO> commodityDetailVOS = list.stream().map(commodity -> {
            CommodityDetail commodityDetail = commodityDetailService.selectByCommodityId(commodity.getId());
            return new CommodityDetailVO(commodity.getId(),
                    commodity.getTitle(),
                    commodity.getPrice(),
                    commodity.getAmount(),
                    commodity.getCreateTime(),
                    commodity.getStatus(),
                    commodity.getIntroduce(),
                    commodityDetail);
        }).collect(Collectors.toList());
        PageBean<CommodityDetailVO> pageBean = new PageBean<>(pageIndex <= 0 ? 1 : pageIndex, pageSize);
        pageBean.setTotalCount(count);
        pageBean.setResultList(commodityDetailVOS);
        return pageBean;
    }

    @Transactional
    @Override
    public int insert(CommodityDetailVO record) {
        record.setCreateTime(new Date());
        commodityMapper.insertSelective(record);
        CommodityDetail commodityDetail = record.getCommodityDetail();
        commodityDetail.setCommodityId(record.getId());
        commodityDetailService.insertSelective(commodityDetail);
        return 0;
    }

    @Transactional
    @Override
    public int update(CommodityDetailVO record) {
        CommodityDetail commodityDetail = record.getCommodityDetail();
        commodityDetailService.updateByPrimaryKeySelective(commodityDetail);
        commodityMapper.updateByPrimaryKeySelective(record);
        return 0;
    }

    @Override
    public int update(Commodity record) {
        return commodityMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(Long id) throws DemoException {
        Commodity commodity = commodityMapper.selectByPrimaryKey(id);
        CommodityDetail commodityDetail = commodityDetailService.selectByCommodityId(id);
        if (commodity != null && commodityDetail.getSales() > 0) {
            throw new DemoException("商品已有售出记录，无法删除");
        }
        return commodityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CommodityDetailVO selectByPrimaryKey(Long id) {
        Commodity commodity = commodityMapper.selectByPrimaryKey(id);
        CommodityDetail commodityDetail = commodityDetailService.selectByCommodityId(commodity.getId());
        return new CommodityDetailVO(commodity.getId(),
                commodity.getTitle(),
                commodity.getPrice(),
                commodity.getAmount(),
                commodity.getCreateTime(),
                commodity.getStatus(),
                commodity.getIntroduce(),
                commodityDetail);
    }
}
